<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
    <p>请以字符串输入你要猜的四个0到9的数字</p>
	<input type="text" id="text">
	<button id="button" onclick="show()">确定</button><br>
	<span id="span"></span> 
 </body>
 <script>
     var n = 1;
	 var s = 10;
     var arr1 = new Array();
	 for(var i = 0;i < 4;i++){
	   var m = parseInt(Math.random()*10);
	   if(m == 10){
	      m = 9;
	   }
	   arr1[i] = m;
	 }
	alert(arr1);

	 function show(){
	   
       if(s - n == -1){
	       return;
	   }
	    
	   var x = 0;  //记录正确的个数
	   var y = 0;  //记录错误的个数
	
	   var text = document.getElementById("text").value;
	   var span = document.getElementById("span");
	   var arr2 = text.split(""); 
	    
	   for(var j = 0;j < arr1.length;j++){
	      if(arr1[j] == arr2[j]){
		     x++;
		  }
          else{
		    y++;
		  }
	   }
      if(x == 4){
	    span.innerHTML = "恭喜你，猜对了"
		span.style.color = "green";
		n++;
		return;
	  }
	  if(n == 10 && x < 4){
        span.innerHTML = "你已经用完了猜数的机会";
		span.style.color = "red"; 
		n++;
		return;
	  }
	  if(x < 4 && n < 10){
	    span.innerHTML = "猜对了"+x+"个数，猜错了"+y+"个数,你还有"+(s-n)+"次机会";
	  }
	   
	  n ++;
	 }
 </script>
</html>
